import java.util.Arrays;
import java.util.Stack;

/**
 * Created with Intellij JDEA
 * Description:
 * User:
 * Date:2022-03-26
 * Time:10:16
 */
public class TextDemo {
    /**
     * 直接插入排序：
     *
     * @param array
     */
    public static void insertSort(int[] array) {
        int len = array.length;
        int tmp = 0;
        for (int i = 1; i < len; i++) {
            tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    //array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    /**
     * 直接插入排序
     * @param array
     * @param gap
     */
    public static void shell(int[] array,int gap) {
        int len = array.length;
        int tmp = 0;
        for (int i = gap; i < len; i++) {
            tmp = array[i];
            int j = i-gap;
            for (; j >= 0; j-=gap) {
                if (array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    //array[j+1] = tmp;
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }


    public static void shellSort(int[] array) {
        int gap = array.length;//10
        while (gap > 1) {
            gap /= 2;
            shell(array,gap);
        }
        shell(array,1);
    }


    /**
     * 选择排序：
     *
     * @param
     */
    public static void selectSort(int[] array) {
        for (int i = 0; i<array.length ; i++ ){
            int min=i;
            for (int j=i+1;j<array.length ;j++ ){
                if (array[min]>array[j]){
                    min=j;
                }
            }
            if (min!=i){
                int temp=array[i];
                array[i]=array[min];
                array[min]=temp;
            }
        }
    }

    /**
     * 快速排序：
     *
     * @param array
     */
    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    private static void quick(int[] array,int start,int end) {
        if(start >= end) {
            return;
        }
        int pivot = partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }

    /**
     * 一次划分函数
     * @param array
     * @param left
     * @param right
     * @return
     */
    private static int partition(int[] array,int left,int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }
    /**
     * 三数取中法
     * @param array
     * @param left
     * @param right
     * @return 三个数中的 中间数字的下标
     */
    private static int threeMid(int[] array,int left,int right) {
        int mid = (left+right) >>> 1;
        int x = array[mid];
        if (array[left] < array[right]){
            if (array[mid] < array[left]) {
                return left;
            }else if (array[mid] > array[right]) {
                return right;
            }else {
                return mid;
            }
        }else {
            if (array[mid] > array[left]) {
                return left;
            }else if (array[mid] < array[right]) {
                return right;
            }else {
                return mid;
            }
        }
    }

    /**
     * 快速排序：非递归实现
     * @param array
     */
    public static void quickSort2(int[] array) {
        Stack<Integer> stack = new Stack<>();

        int start = 0;
        int end = array.length-1;
        int pivot = partition(array,start,end);

        if(pivot > start+1) {
            stack.push(start);
            stack.push(pivot-1);
        }

        if(pivot < end-1) {
            stack.push(pivot+1);
            stack.push(end);
        }


    }



    public static void main(String[] args) {
        int[] array = new int[]{10,3,6,4,7,45,23,34,54,2,1};
        shellSort(array);
        System.out.println(Arrays.toString(array));


    }
}
